//
//  UpdateHintView.m
//  Grammar
//
//  Created by Rick on 6/26/13.
//
//

#import "UpdateHintView.h"
#import "SyncGramarsDataRequest.h"
#import "SyncKeywordDataRequest.h"
#import "SyncTagDataRequest.h"

#define SYNC_GRAMMAR_DATA_REQUEST   @"SYNC_GRAMMAR_DATA_REQUEST"
#define SYNC_TAG_DATA_REQUEST       @"SYNC_TAG_DATA_REQUEST"
#define SYNC_KEYWORD_DATA_REQUEST   @"SYNC_KEYWORD_DATA_REQUEST"

@implementation UpdateHintView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (float)progress {
    return _progressNumber;
}

- (void)setProgressNumber:(float)progressNumber
{
    _progressNumber = progressNumber;
    
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.bounds = CGRectMake(0, 0, 320, 35);
//    _circularProgressView = [[CircularProgressView alloc] initWithFrame:CGRectMake(294.0f, 8.0f, 25.0f, 25.0f)];
//    _circularProgressView.trackTintColor = [UIColor clearColor];
//    _circularProgressView.progressTintColor = [UIColor whiteColor];
//    _circularProgressView.progress = 0;
//    _circularProgressView.hidden = YES;
//    [self addSubview:_circularProgressView];
    
}

- (void)setUpdateType:(UpdateViewType)updateType
{
    _updateType = updateType;
    switch (_updateType) {
        case UpdateViewTypeCheckUpdate:
        {
            _updateHintLabel.text = @"正在检查更新...";
            _updatButton.enabled = NO;
            _loadActivityIndicator.hidden = NO;
        }
            break;
        case UpdateViewTypeNeedUpdate:
        {
            _updateHintLabel.text = @"知识框架有更新，点击更新";
            _updatButton.enabled = YES;
            _loadActivityIndicator.hidden = YES;
        }
            break;
        case UpdateViewTypeNewest:
        {
            _updateHintLabel.text = @"知识框架已经是最新";
            _updatButton.enabled = NO;
//            [self performSelector:@selector(done) withObject:nil afterDelay:2];
            _loadActivityIndicator.hidden = YES;
        }
            break;
        case UpdateViewTypeNotNetwork:
        {
            _updateHintLabel.text = @"无法检查更新，请检查网络";
            _updatButton.enabled = NO;
//            [self performSelector:@selector(done) withObject:nil afterDelay:2];
            _loadActivityIndicator.hidden = YES;
        }
            break;
        case UpdateViewTypeFailed:
        {
            _updateHintLabel.text = @"更新失败，点击重试";
            _updatButton.enabled = YES;
            _loadActivityIndicator.hidden = YES;
           _updatButton.userInteractionEnabled = YES;
        }
            break;
        default:
            break;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

#pragma mark - IBAction
- (IBAction)tapUpdateKnowledgeButton:(id)sender {
    UIButton *button = (UIButton*)sender;
    button.userInteractionEnabled = NO;
    _loadActivityIndicator.hidden = NO;
//    _circularProgressView.hidden = NO;
    _updateHintLabel.text = @"正在更新...";
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *wholeString = (NSString*)[[DataCacheManager sharedManager] getCachedObjectByKey:WHOLE_VERSION];
    
    [parameters setObject:wholeString forKey:@"wholeVersion"];
    [SyncGramarsDataRequest requestWithParameters:parameters withIndicatorView:nil withCancelSubject:nil onRequestStart:nil onRequestFinished:^(ITTBaseDataRequest *request) {
        [self syncKeywordDataRequest];
    } onRequestCanceled:^(ITTBaseDataRequest *request) {
        [self setUpdateType:UpdateViewTypeFailed];
    } onRequestFailed:^(ITTBaseDataRequest *request) {
        [self setUpdateType:UpdateViewTypeFailed];
    }];
    //多线程启动定时器
//    [NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil];
   
}
- (void)startTimer
{
//     _timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(progressChange) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] run];
}
- (void)syncKeywordDataRequest
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *wholeString = (NSString*)[[DataCacheManager sharedManager] getCachedObjectByKey:WHOLE_VERSION];
    [parameters setObject:wholeString forKey:@"wholeVersion"];
    [SyncKeywordDataRequest requestWithParameters:parameters withIndicatorView:nil withCancelSubject:nil onRequestStart:nil onRequestFinished:^(ITTBaseDataRequest *request) {
        [self syncTagDataRequest];
    } onRequestCanceled:^(ITTBaseDataRequest *request) {
        [self setUpdateType:UpdateViewTypeFailed];
    } onRequestFailed:^(ITTBaseDataRequest *request) {
        [self setUpdateType:UpdateViewTypeFailed];
    }];

}

- (void)syncTagDataRequest
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    NSString *wholeString = (NSString*)[[DataCacheManager sharedManager] getCachedObjectByKey:WHOLE_VERSION];
    [parameters setObject:wholeString forKey:@"wholeVersion"];
    [SyncTagDataRequest requestWithParameters:parameters withIndicatorView:nil withCancelSubject:nil onRequestStart:nil onRequestFinished:^(ITTBaseDataRequest *request) {
         [self done];
    } onRequestCanceled:^(ITTBaseDataRequest *request) {
        [self setUpdateType:UpdateViewTypeFailed];
    } onRequestFailed:^(ITTBaseDataRequest *request) {
        [self setUpdateType:UpdateViewTypeFailed];
    }];
}

- (void)progressChange
{
//    _circularProgressView.progress += 0.01;
//    if (_circularProgressView.progress > 1.0f)
//    {
//        _circularProgressView.progress = 0.0f;
//    }
//    if (_circularProgressView.progress >=1.0f) {
//        _circularProgressView.progress= 1.0f;
//        [self done];
//        return;
//    }
}

- (void)stopRequest
{
    [[NSNotificationCenter defaultCenter] postNotificationName:SYNC_GRAMMAR_DATA_REQUEST object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:SYNC_KEYWORD_DATA_REQUEST object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:SYNC_TAG_DATA_REQUEST object:nil];
}

- (void)done
{
    DATA_ENV.isNeedUpdateRequest = NO;
    _loadActivityIndicator.hidden = YES;
    _updateHintLabel.text = @"知识框架已经是最新";
    _updatButton.enabled = NO;
//    [self removeFromSuperview];
}
- (void)dealloc {
    [_updatButton release];
    [_loadActivityIndicator release];
    [super dealloc];
}
@end
